package org.batatasframework.chart.columns;

import java.util.Iterator;

import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.batatasframework.chart.ChartCell;
import org.batatasframework.chart.data.ChartInitializer;
import org.batatasframework.chart.graphics.ChartGraphic;

/**
 * 直接把几个字段组成一个返回的字段来处理
 * @author jackson
 *
 */
public abstract class ChartColumnGroupable<T extends ChartColumn> extends AbstractChartColumn{
	
	protected final T[] columns;
	
	public static ChartColumnAxis axixGroup(String name, ChartColumnAxis... columns){
		return new ColumnAxisGroup(name, columns);
	}
	
	public static ChartColumnValue valueGroup(String name, ChartColumnValue... columns){
		return new ColumnValueGroup(name, columns);
	}

//	@Override
	public Iterator<T> iterator() {
		return new InternalIterator<>(columns);
	}
	
	protected ChartColumnGroupable(String name, T[] columns) {
		super(name);
		this.columns = columns;

		for(ChartColumn column : columns){
			if(column.rowspan() > rowspan) rowspan = column.rowspan();
		}
		//这里本身就是组合，所以head必须会占多一行的位置
		this.rowspan++;
		
	}
	
	@Override
	public int size() {
		int size = 0;
		for(ChartColumn column : columns){
			size += column.size();
		}
		return size;
	}
	
	@Override
	public ChartCell newCell() {
		ChartCell[] mcCells = new ChartCell[columns.length];
		int i = 0;
		for(ChartColumn column : columns){
			mcCells[i++] = column.newCell();
		}
		return new MultipeChartCell(mcCells);
	}

	@Override
	public void resolveHead(String[] heads, int deep) {
		super.resolveHead(heads, deep);
		for(ChartColumn column : columns){
			column.resolveHead(heads, deep + 1);
		}
	}
	
	@Override
	public void resolveHead(XSSFRow[] heads, XSSFCellStyle style, int deep) {
		super.resolveHead(heads, style, deep);
		for(ChartColumn column : columns){
			column.resolveHead(heads, style, deep + 1);
		}
	}

	@Override
	public ChartColumn initialize(ChartInitializer initializer) {
		this.ordinal = initializer.nextOrdinal();
		
		for(ChartColumn column : columns){
			column.initialize(initializer);
		}
		
		return this;
	}
	
}
class ColumnAxisGroup extends ChartColumnGroupable<ChartColumnAxis> implements ChartColumnAxis, ChartColumnAxisIterable{

	protected ColumnAxisGroup(String name, ChartColumnAxis[] columns) {
		super(name, columns);
	}
	
}

class ColumnValueGroup extends ChartColumnGroupable<ChartColumnValue> implements ChartColumnValue, ChartColumnValueIterable{

	private String unit;
	
	protected ColumnValueGroup(String name, ChartColumnValue[] columns) {
		super(name, columns);
	}

	@Override
	public ChartColumnValue graphic(ChartGraphic... graphics) {
		for(ChartColumnValue column : columns){
			column.graphic(graphics);
		}
		return this;
	}
	
	@Override
	public ChartColumnValue setUnit(String unit) {
		this.unit = unit;
		return this;
	}
	
	@Override
	public String toString() {
		String value = super.toString();
		if(null != unit) value += "(" + unit + ")";
		return value;
	}
}
